﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace StepPrice
{
    /****************************/
    /****************************/
    /***三阶计算，pjt,20160721，v1.0***/
    /***增加多人口、换表结转气量的计算方式，pjt,20170419 v2.0 ***/
    /****************************/
    /****************************/
    /****************************/
    public class CalcStepPrice
    {
        StepPriceInfo info = new StepPriceInfo();
        private decimal limitGasCount1 = 0;//一阶限量（设置的限量*月数）
        private decimal limitGasCount2 = 0;//二阶限量（设置的限量*月数）
        public CalcStepPrice(StepPriceInfo _info)
        {
            info = _info;
        }

        /// <summary>
        /// 计算月数
        /// </summary>
        private int CalcMonthCount()
        {
            int monthCount = 0;
            if (info.FlagGasAver)
            {
                monthCount = 1;
            }
            else
            {
                int flag = -1;
                string[] datePan = ShowDatePan().Split('#');
                for (int i = 0; i < datePan.Length; i++)
                {
                    if (DateTime.Now.Year == int.Parse(datePan[i].Substring(0, 4)))
                    {
                        flag = i;
                        break;
                    }
                }
                if (flag != -1)
                {

                    if (info.OpenGasDate.Length == 8)
                    {
                        int openGasYear = int.Parse(info.OpenGasDate.Substring(0, 4));
                        int openGasMonth = int.Parse(info.OpenGasDate.Substring(4, 2));
                        //  int openGasDay = int.Parse(info.OpenGasDate.Substring(6, 2));

                        if (openGasYear < int.Parse(datePan[flag].Substring(0, 4)) || (int.Parse(openGasYear + openGasMonth.ToString().PadLeft(2, '0')) < int.Parse(DateTime.Parse(datePan[flag].Split(',')[0]).ToString("yyyy-MM-dd").Replace("-", "").Substring(0, 6))))
                        {
                            monthCount = DateTime.Parse(datePan[flag].Split(',')[1]).Month - DateTime.Parse(datePan[flag].Split(',')[0]).Month + 1;

                        }
                        else
                        {
                            monthCount = DateTime.Parse(datePan[flag].Split(',')[1]).Month - openGasMonth + 1;

                        }

                    }
                    else
                    {
                        monthCount = DateTime.Parse(datePan[flag].Split(',')[1]).Month - DateTime.Parse(datePan[flag].Split(',')[0]).Month + 1;

                    }
                }

            }
            return monthCount;

        }

        /// <summary>
        /// 计算每阶限量
        /// </summary>
        private void CalcLimitGas()
        {
            int monthCount = CalcMonthCount();
            limitGasCount1 = info.LimitGas1 * monthCount;
            limitGasCount2 = info.LimitGas2 * monthCount;
            if (info.RenDingGas > 0 && info.JieZhuanGas > 0)
            {
                GetMeiJieGasByJiKa();
            }

        }

        /// <summary>
        /// 计算每阶限量
        /// </summary>
        /// <param name="limi1"></param>
        /// <param name="limi2"></param>
        public void CalcLimitGas(out decimal limi1, out decimal limi2)
        {
            int monthCount = CalcMonthCount();
            limi1 = info.LimitGas1 * monthCount;
            limi2 = info.LimitGas2 * monthCount;

        }

        /// <summary>
        /// 计算开始日期与结束日期 各时间段（按年统计）
        /// </summary>
        /// <returns>"开始日期,结束日期#开始日期,结束日期"</returns>
        public string ShowDatePan()
        {
            DateTime startDate = info.StepStartDate;
            DateTime endDate = info.StepEndDate;
            int yearCount = endDate.Year - startDate.Year + 1;
            string str = "";
            if (yearCount > 1)
            {
                for (int i = 0; i < yearCount; i++)
                {
                    if (startDate.Year + i == startDate.Year)
                    {
                        str += startDate.ToString("yyyy-MM-dd") + "," + startDate.Year + "-12-31" + "#";
                    }
                    else if (startDate.Year + i == endDate.Year)
                    {
                        str += (startDate.Year + i) + "-01-01" + "," + endDate.ToString("yyyy-MM-dd") + "#";
                    }
                    else
                    {
                        str += (startDate.Year + i) + "-01-01" + "," + (startDate.Year + i) + "-12-31" + "#";
                    }

                }
            }
            else
            {
                str += startDate.ToShortDateString() + "," + endDate.ToShortDateString() + "#";
            }
            return str.Substring(0, str.Length - 1);
        }


        /// <summary>
        /// 通过购买量计算各阶气量与气费
        /// </summary>
        private StepPriceInfo CalcStep_Gas()
        {
            decimal nianCount = info.GasAll;//用户年累计气量
            var payGas = info.PayGas;//输入的购气量
            decimal money1 = 0, money2 = 0, money3 = 0, gas1 = 0, gas2 = 0, gas3 = 0;

            if (info.RenDingGas > 0)
            {
                nianCount = nianCount - info.RenDingGas - info.LeiJiGas2 - info.LeiJiGas3;//用户年累计气量
            }
            //判断一阶
            if ((nianCount + payGas) <= limitGasCount1)
            {
                gas1 = payGas;//一阶气量
            }
            else
            {
                if (nianCount <= limitGasCount1)//年累计<=一阶限量？
                {
                    gas1 = limitGasCount1 - nianCount;//一阶气量
                }
                if ((nianCount + payGas) > limitGasCount2)//年累计+输入气量 >二阶限量
                {
                    if (nianCount <= limitGasCount2)//年累计<=二阶限量？
                    {
                        gas2 = limitGasCount2 - nianCount - gas1;
                    }
                    //三阶气量=（充值金额-一阶气费-二阶气费）/三阶单价
                    //三阶气费=三阶气量*三阶单价
                    gas3 = payGas - gas1 - gas2;

                }
                else
                {
                    gas2 = payGas - gas1;
                }
            }


            money1 = Math.Round(info.Price1 * gas1, 2);//一阶气费=一阶单价*一阶气量
            money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量
            money3 = Math.Round(info.Price3 * gas3, 2);//三阶气费=三阶单价*三价气量
            decimal payMoney = money1 + money2 + money3 - info.AccountBalance;
            var _info = new StepPriceInfo()
            {
                Gas1 = gas1,
                Gas2 = gas2,
                Gas3 = gas3,
                Money1 = money1,
                Money2 = money2,
                Money3 = money3,
                PayMoney = payMoney,
                PayGas = payGas,
                AccountBalance = 0
            };
            return _info;
        }

        /// <summary>
        /// 通过交费金额计算各阶气量与气费
        /// </summary>
        private StepPriceInfo CalcStep_Money()
        {
            decimal payGas = 0;
            decimal payMoneyTemp = info.PayMoney + info.AccountBalance;
            decimal payMoney = info.PayMoney;
            decimal nianCount = info.GasAll;
            var gas = (int)(payMoneyTemp / info.Price1); //按一阶单价计算能购气的气量
            decimal money1 = 0, money2 = 0, money3 = 0, gas1 = 0, gas2 = 0, gas3 = 0, accountBalance = 0;


            if (info.RenDingGas > 0)
            {
                nianCount = nianCount - info.RenDingGas - info.LeiJiGas2 - info.LeiJiGas3;//用户年累计气量
            }
            //判断一阶
            if ((nianCount + gas) <= limitGasCount1)
            {
                gas1 = gas;
                money1 = Math.Round(info.Price1 * gas1, 2);
                accountBalance = payMoneyTemp - money1;
            }
            else
            {
                if (nianCount <= limitGasCount1)//年累计<=一阶限量？
                {
                    gas1 = (int)(limitGasCount1 - nianCount);
                    money1 = Math.Round(info.Price1 * gas1, 2);//一阶气费=一阶单价*一价气量

                }
                if (nianCount + gas1 + Math.Round((payMoneyTemp - money1) / info.Price2, 2) > limitGasCount2)
                {
                    if (nianCount <= limitGasCount2)//年累计<=二阶限量？
                    {
                        gas2 = (limitGasCount2 - nianCount - gas1);
                        money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量
                        accountBalance = (payMoneyTemp - money1 - money2);
                    }
                    //三阶气量=（充值金额-一阶气费-二阶气费）/三阶单价
                    //三阶气费=三阶气量*三阶单价
                    gas3 = (int)((payMoneyTemp - money1 - money2) / info.Price3);
                    money3 = Math.Round(info.Price3 * gas3, 2);
                    accountBalance = (payMoneyTemp - money1 - money2 - money3);
                }
                else
                {
                    gas2 = ((int)Math.Round((payMoneyTemp - money1) / info.Price2, 2));

                    money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量

                    accountBalance = payMoneyTemp - money1 - money2;
                }
            }
            payGas = gas1 + gas2 + gas3;
            var _info = new StepPriceInfo()
            {
                Gas1 = gas1,
                Gas2 = gas2,
                Gas3 = gas3,
                Money1 = money1,
                Money2 = money2,
                Money3 = money3,
                PayMoney = payMoney,
                PayGas = payGas,
                AccountBalance = accountBalance
            };
            return _info;
        }


        /// <summary>
        /// 通过购买量计算各阶气量与气费（多人口）
        /// </summary>
        private StepPriceInfo CalcStepPopu_Gas()
        {

            bool flag2 = false;//用户所用气量已达到二阶
            bool flag3 = false;//用户所用气量已达到三阶

            decimal limitGasCount1Temp = limitGasCount1;

            decimal nianCount = info.GasAll;//用户年累计气量
            var payGas = info.PayGas;//输入的购气量

            decimal money1 = 0, money2 = 0, money3 = 0, gas1 = 0, gas2 = 0, gas3 = 0;


            if (info.LeiJiGas3 > 0)
            {
                flag3 = true;

            }
            if (info.LeiJiGas2 > 0)
            {
                flag2 = true;
            }
            //if (flag3 || flag2)
            //{

            limitGasCount1 += info.RenDingGas;

            //}
            //else
            //{
            //    limitGasCount1 +=  info.RenDingGas;
            //}
            //判断一阶
            if ((nianCount + payGas) <= limitGasCount1)
            {
                gas1 = payGas;//一阶气量

            }
            else
            {
                if ((payGas - info.RenDingShengYuGas) <= limitGasCount2 - limitGasCount1Temp - info.LeiJiGas2 && !flag3)
                {
                    if (flag2)
                    {
                        gas1 = info.RenDingShengYuGas;//一阶气量
                    }
                    else
                    {
                        gas1 = limitGasCount1 - info.LeiJiGas1;//一阶气量
                    }

                    gas2 = payGas - gas1;//二阶气量
                    gas3 = 0;//三阶气量

                }
                else
                {
                    if (flag2 || flag3)
                    {
                        gas1 = info.RenDingShengYuGas;//一阶气量
                        gas2 = limitGasCount2 - limitGasCount1Temp - info.LeiJiGas2;//二阶气量
                        if (flag3)
                        {
                            gas2 = 0;
                        }
                    }
                    else
                    {
                        gas1 = limitGasCount1 - info.LeiJiGas1;

                        gas2 = payGas - gas1;
                        if (gas2 > limitGasCount2 - limitGasCount1Temp)
                        {
                            gas2 = limitGasCount2 - limitGasCount1Temp;
                        }


                    }

                    gas3 = payGas - gas1 - gas2;

                }
            }
            money1 = Math.Round(info.Price1 * gas1, 2);//一阶气费=一阶单价*一阶气量
            money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量
            money3 = Math.Round(info.Price3 * gas3, 2);//三阶气费=三阶单价*三价气量

            decimal payMoney = money1 + money2 + money3 - info.AccountBalance;
            var _info = new StepPriceInfo()
            {
                Gas1 = gas1,
                Gas2 = gas2,
                Gas3 = gas3,
                Money1 = money1,
                Money2 = money2,
                Money3 = money3,
                PayMoney = payMoney,
                PayGas = payGas,
                AccountBalance = 0
            };
            return _info;
        }




        /// <summary>
        /// 通过交费金额计算各阶气量与气费（多人口）
        /// </summary>
        private StepPriceInfo CalcStepPopu_Money()
        {
            decimal payGas = 0;
            decimal payMoney = info.PayMoney + info.AccountBalance;
            decimal payMoneyTemp = info.PayMoney;
            decimal nianCount = info.GasAll;
            //   var gas = (int)(payMoneyTemp / info.Price1); //按一阶单价计算能购气的气量
            decimal money1 = 0, money2 = 0, money3 = 0, gas1 = 0, gas2 = 0, gas3 = 0, accountBalance = 0;


            decimal renDingMoney = Math.Round(info.RenDingGas * info.Price1, 2);

            if (payMoney <= renDingMoney + (limitGasCount1 - info.LeiJiGas1) * info.Price1)
            {

                gas1 = (int)(payMoney / info.Price1); //一阶购气量

                money1 = Math.Round(gas1 * info.Price1, 2);//一阶气费

                accountBalance = payMoney - money1;
            }
            else
            {
                gas1 = info.RenDingGas + limitGasCount1 - info.LeiJiGas1; //一阶购气量
                money1 = Math.Round(info.Price1 * gas1, 2);//一阶气费=一阶单价*一价气量;


                gas2 = (int)((payMoney - money1) / info.Price2); //剩余金额，所能够买的二阶量
                if (gas2 > limitGasCount2 - limitGasCount1 - info.LeiJiGas2)
                {

                    gas2 = (limitGasCount2 - limitGasCount1 - info.LeiJiGas2);//二阶购气量
                    money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量



                    //三阶气量=（充值金额-一阶气费-二阶气费）/三阶单价
                    //三阶气费=三阶气量*三阶单价
                    gas3 = (int)((payMoney - money1 - money2) / info.Price3);
                    money3 = Math.Round(info.Price3 * gas3, 2);
                    accountBalance = payMoney - money1 - money2 - money3;
                }
                else
                {
                    money2 = Math.Round(info.Price2 * gas2, 2);//二阶气费=二阶单价*二价气量
                    accountBalance = (payMoney - money1 - money2);
                }

            }
            payGas = gas1 + gas2 + gas3;
            var _info = new StepPriceInfo()
            {
                Gas1 = gas1,
                Gas2 = gas2,
                Gas3 = gas3,
                Money1 = money1,
                Money2 = money2,
                Money3 = money3,
                PayMoney = payMoneyTemp,
                PayGas = payGas,
                AccountBalance = accountBalance
            };
            return _info;
        }

        /// <summary>
        /// 计算阶梯收费
        /// </summary>
        public StepPriceInfo CalcStep()
        {
            if (DateTime.Now >= info.StepStartDate && DateTime.Now <= info.StepEndDate)
            {
                StepPriceInfo stepPriceInfo = new StepPriceInfo();
                CalcLimitGas();

                if (info.FlagPayWay)
                {
                    stepPriceInfo = info.RenDingShengYuGas > 0 ? CalcStepPopu_Money() : CalcStep_Money();

                }
                else
                {
                    stepPriceInfo = info.RenDingShengYuGas > 0 ? CalcStepPopu_Gas() : CalcStep_Gas();
                }
                return stepPriceInfo;

            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 重新计算每阶累计量
        /// </summary>
        private void GetMeiJieGasByJiKa()
        {
            decimal jiKaGas;
            if (info.GasAll > (info.LeiJiGas1 + info.LeiJiGas2 + info.LeiJiGas3))
            {
                jiKaGas = info.GasAll - (info.LeiJiGas1 + info.LeiJiGas2 + info.LeiJiGas3);

                if (jiKaGas > limitGasCount2)
                {
                    info.LeiJiGas3 += Convert.ToInt32(jiKaGas - limitGasCount2);
                    jiKaGas = jiKaGas - limitGasCount2;
                    if (jiKaGas > limitGasCount1)
                    {
                        info.LeiJiGas2 += Convert.ToInt32(jiKaGas - limitGasCount1);
                        jiKaGas = jiKaGas - limitGasCount1;
                        info.LeiJiGas1 += Convert.ToInt32(jiKaGas);
                    }
                }
                else if (jiKaGas > limitGasCount1)
                {
                    info.LeiJiGas2 += Convert.ToInt32(jiKaGas - limitGasCount1);
                    jiKaGas = jiKaGas - limitGasCount1;
                    info.LeiJiGas1 += Convert.ToInt32(jiKaGas);
                }
                else
                {
                    info.LeiJiGas1 += Convert.ToInt32(jiKaGas);
                }
            }
        }

    }
}
